package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.ZoneMapper;
import zhongbo.java.air.dictionary.model.Zone;
import zhongbo.java.air.dictionary.service.IZoneService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ZoneServiceImpl implements IZoneService {
    @Autowired
    private ZoneMapper zoneMapper;
    List<Zone>list=new ArrayList<>();

    @Override
    public Zone add(Zone entity) {
        ValidAirPort(entity);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        int insert=zoneMapper.insert(entity);
        if (insert>0){
            list.add(entity);
            return entity;
        }
        throw new RuntimeException("添加失败");
    }
    private void ValidAirPort(Zone zone){
        String AirId=zone.getId();
        if ((StrUtil.isBlank(AirId))){
            throw new RuntimeException("id不能为空");
        }
        String zone_type=zone.getZoneCode();
        if (StrUtil.isBlank(zone_type) || zone_type.length()>7){
            throw new RuntimeException("区域业务编号不能为空且不能大于7位");
        }
        for (Zone extZone:list){
           if (extZone.getZoneCode().equals(zone.getZoneCode())){
                throw new RuntimeException("区域业务编号不能重复");
            }
        }
    }

    @Override
    public String delete(String entityId) {
        if (StrUtil.isBlank(entityId)) {
            throw new RuntimeException("id为空");
        }
        int delete = zoneMapper.deleteById(entityId);
        if (delete > 0) {
            return entityId;
        }
        throw new RuntimeException("删除失败 ! id:"+entityId);
    }

    @Override
    public Zone edit(Zone edit) {
        ValidAirPort(edit);
        edit.setUpdateTime(new Date());
        int editAir = zoneMapper.updateById(edit);
        if (editAir > 0) {
            list.add(edit);
            return edit;
        }
        throw new RuntimeException("修改失败");
    }

    @Override
    public Zone findAirPort(String entityId) {
        if (StrUtil.isBlank(entityId)) {
            throw new RuntimeException("id不能没有");
        }
        return zoneMapper.selectById(entityId);
    }

    @Override
    public PageVo<Zone> pageVo(Integer page, Integer pageSize
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<Zone> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<Zone> pageParam = new Page<>(page, pageSize);
        Page<Zone> userPage = zoneMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userPage);

    }
}
